package com.mark.web.bill.controller;

import com.mark.common.ServerResponse;
import com.mark.web.bill.entity.from.AddGoodsCateFrom;
import com.mark.web.bill.entity.from.EditGoodsCateFrom;
import com.mark.web.bill.entity.po.GoodsCategory;
import com.mark.web.bill.entity.vo.GoodsCategoryVo;
import com.mark.web.bill.service.IGoodsCateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 加工零件种类控制器
 * @Author: Mark
 * @CreateDate: 2019/2/16 21:13
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "账单-加工零件种类模块")
@RestController
@RequestMapping(value = "/bill/goodsCate/")
public class GoodsCateController {

    private final IGoodsCateService iGoodsCateService;

    @Autowired
    public GoodsCateController(IGoodsCateService iGoodsCateService) {
        this.iGoodsCateService = iGoodsCateService;
    }

    @ApiOperation(value = "获取加工零件同一个父级的种类信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId",value = "种类父级ID",defaultValue = "0")
    })
    @PostMapping(value = "getGoodsCates")
    public ServerResponse<List<GoodsCategoryVo>> getGoodsCateList(@RequestParam(value = "parentId",defaultValue = "0") int parentId){
        return iGoodsCateService.getGoodsCateList(parentId);
    }

    @ApiOperation(value = "搜索获取加工零件同一个父级的种类信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId",value = "搜索种类父级ID"),
            @ApiImplicitParam(name = "CategoryName",value = "搜索种类名称")
    })
    @PostMapping(value = "getSearchGoodsCates")
    public ServerResponse<List<GoodsCategoryVo>> getGoodsCateList(Integer parentId,String CategoryName){
        return iGoodsCateService.getSearchGoodsCates(parentId,CategoryName);
    }

    @ApiOperation(value = "获取渲染种类Cascader级联选择器的数据")
    @GetMapping(value = "getGoodsCateCascader")
    public  ServerResponse<List<Map<String,Object>>> getGoodsCateCascader(){
        return iGoodsCateService.getGoodsCateCascader();
    }

    @ApiOperation(value = "添加种类")
    @PostMapping(value = "add")
    public ServerResponse<String> add(@Valid AddGoodsCateFrom addGoodsCateFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        GoodsCategory goodsCategory = new GoodsCategory();
        BeanUtils.copyProperties(addGoodsCateFrom,goodsCategory);
        return iGoodsCateService.add(goodsCategory);
    }

    @ApiOperation(value = "编辑种类")
    @PostMapping(value = "edit")
    public ServerResponse<String> edit(@Valid EditGoodsCateFrom editGoodsCateFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        GoodsCategory goodsCategory = new GoodsCategory();
        BeanUtils.copyProperties(editGoodsCateFrom,goodsCategory);
        return iGoodsCateService.edit(goodsCategory);
    }
}
